<?php
$this->pageTitle = 'Manage Products';
$this->breadcrumbs = array(
    'Products' => array('index'),
    'Manage',
);



//$this->menu=array(
//	array('label'=>'Create Product', 'url'=>array('create')),
//);
//
//ShortUtil::registerScript('search', "
//$('.search-button').click(function(){
//	$('.search-form').toggle();
//	return false;
//});
//$('.search-form form').submit(function(){
//	$.fn.yiiGridView.update('product-grid', {
//		data: $(this).serialize()
//	});
//	return false;
//});
//
//");
?>
<div class="heading">
    <h1><img src="<?php echo ShortUtil::themePath() ?>/images/default.png" /> Manage Products</h1>

    <div class="controls">
<?php
echo CHtml::button('Create', array('class' => 'controls', 'id' => 'create-product'));
echo CHtml::button('Delete', array('class' => 'controls', 'id' => 'delete-product'));
?>
    </div>
</div>

<div class="content">

<?php
// echo CHtml::link('Advanced Search','#',array('class'=>'search-button'));
// echo '<div class="search-form" style="display:none">';
// $this->renderPartial('_search',array(
//	'model'=>$model,
// ));
// echo '</div><!-- search-form -->';
?>


<?php
$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'product-grid',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'cssFile' => ShortUtil::themePath() . '/css/gridview.css',
    'htmlOptions' => array('class' => 'grid-view clear'),
    'afterAjaxUpdate' => 'js:function(id,data){$.bind_data()}',
    'columns' => array(
        array(
            'class' => 'CCheckBoxColumn',
            'selectableRows' => 2,
            'id' => 'chk',
            'value' => $model->prod_id,
        ),
        array(
            'header' => 'No.',
            'value' => '$row + 1',
            'htmlOptions' => array(
                'style' => 'width:25px; text-align:center'
            )
        ),
        'prod_name',
        'img_name',
        'short_desc',
        /*
          'tag',
         */
        array(
            'class' => 'CButtonColumn',
            'viewButtonImageUrl' => false,
            'updateButtonImageUrl' => false,
            'deleteButtonImageUrl' => false,
            'template' => '[{view}] [{update}]'
        ),
    ),
));
?>
</div>

<script  type="text/javascript">
    $(document).ready(function() {
        $. bind_data = function(){
            $(".grid-view a.view").click(function() {
                var url = $(this).attr('href') + '?ajax=true';
                $.fancybox.open({
                    href : url,
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('product-grid')
                    }
                });
                return false;
            });

            $(".grid-view a.update").click(function() {
                var url = $(this).attr('href') + '?ajax=true';
                $.fancybox.open({
                    href : url,
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('product-grid')
                    }
                });
                return false;
            });
        }

        // bind data
        $. bind_data();

        $("#create-product").click(function() {
            $.fancybox.open({
                href : '<?php echo $this->createUrl('create', array('ajax' => true)) ?>',
                type : 'iframe',
                afterClose: function(){
                    $.fn.yiiGridView.update('product-grid')
                }
            });
            return false;
        });

        $('#delete-product').live('click', function(){
            var count=0;
            var params = 'YII_CSRF_TOKEN=<?php echo Yii::app()->request->csrfToken; ?>';
            $("input[name='chk[]']").each(function(){
                if($(this).is(":checked")){
                    count++;
                    params += '&chk[]=' + $(this).val();
                }
            });
            if(count >= 1 ){
                if(!confirm("Delete selected records"))
                    return false;
            }else{
                alert("No checkbox is selected");
                return false;
            }

            $.ajax({
                type: "POST",
                url: '<?php echo $this->createUrl('delete', array('ajax' => true)) ?>',
                data: params,
                success: function(data) {
                    $.fn.yiiGridView.update('product-grid');
                } //success
            });//ajax
            return false;
        });
    });
</script>